package com.v9.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.v9.common.core.service.DefaultService;
import com.v9.common.exception.BizException;
import com.v9.common.utils.ValuePare;
import com.v9.system.domain.RoleInfo;
import com.v9.system.domain.RoleMenu;
import com.v9.system.domain.UserRole;
import com.v9.system.mapper.RoleInfoMapper;
import com.v9.system.mapper.RoleMenuMapper;
import com.v9.system.mapper.UserRoleMapper;
import com.v9.system.service.RoleInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 角色 业务层处理
 *
 * @author shilun
 */
@Service
public class RoleInfoServiceImpl extends DefaultService<RoleInfoMapper, RoleInfo> implements RoleInfoService {




    public List<RoleInfo> queryByPin(String pin) {
        return getBaseMapper().queryByPin(pin);
    }

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public String buildTypeCode() {
        return "roleType";
    }

    @Override
    public String buildTypeName() {
        return "角色列表";
    }

    @Override
    public List<ValuePare> buildData() {
        List<ValuePare> results=new ArrayList<>();
        List<RoleInfo> roleInfos = queryAll();
        roleInfos.forEach(item->{
            ValuePare pare=new ValuePare(item.getId(),item.getName());
            results.add(pare);
        });
        return results;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(RoleInfo role, Long... menuIds) {
        this.saveOrInsert(role);
        List<Long> roleMenus = new ArrayList<>();
        Collections.addAll(roleMenus, menuIds);
        List<RoleMenu> newRoleMenu=new ArrayList<>();
        for(Long menuId:menuIds){
            RoleMenu roleMenu=new RoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(role.getId());
            newRoleMenu.add(roleMenu);
        }

        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", role.getId());
        List<RoleMenu> oldMenuIds = roleMenuMapper.selectList(queryWrapper);

        Collection<RoleMenu> addRoleMenus = CollectionUtils.subtract(newRoleMenu, oldMenuIds);
        addRoleMenus.forEach(item->{
            roleMenuMapper.insert(item);
        });

        Collection<RoleMenu> delRoleMenu = CollectionUtils.subtract(oldMenuIds, newRoleMenu);
        delRoleMenu.forEach(item->{
            QueryWrapper<RoleMenu> query = new QueryWrapper<>();
            query.eq("role_id", role.getId());
            query.eq("menu_id", item.getMenuId());
            roleMenuMapper.delete(query);
        });
    }

    @Override
    public void verifyDel(Long id) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        List<UserRole> userRoles = userRoleMapper.selectList(queryWrapper);
        if (userRoles != null && userRoles.size() > 0) {
            throw new BizException("已有用户在使用，不能删除角色");
        }
        QueryWrapper<RoleMenu> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("role_id",id);
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(queryWrapper1);
        if(roleMenus!=null&&roleMenus.size()>0){
            throw new BizException("已有菜单在引用，不能删除角色");
        }
    }
}
